package richinfo.delivery.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public class PropertiesUtil {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(PropertiesUtil.class);

	private static Properties props = new Properties();

	private static PropertiesUtil pUtil=new PropertiesUtil();
	
	private PropertiesUtil() {

	}

	public static PropertiesUtil getInstance() {
		return pUtil;
	}

	public static void loadProps(String propsPath) {
		InputStream inStream = null;
		try {
			inStream = new FileInputStream(propsPath);
			props.load(inStream);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
		} finally {
			if (null != inStream) {
				try {
					inStream.close();
					inStream = null;
				} catch (Exception e) {
				}
			}
		}
	}

	/**
	 * get string
	 * 
	 * @param key
	 * @return
	 */
	public String getString(String key) {
		String temp = props.getProperty(key);
		if (!AssertUtil.isNullOrEmpty(temp)) {
			return temp.trim();
		}
		return temp;
	}

	/**
	 * get string
	 * 
	 * @param key
	 * @param defVal
	 * @return
	 */
	public String getString(String key, String defVal) {
		String temp = props.getProperty(key, defVal);
		if (!AssertUtil.isNullOrEmpty(temp)) {
			return temp.trim();
		}
		return temp;
	}

	/**
	 * get int
	 * 
	 * @param key
	 * @return
	 */
	public int getInt(String key) {
		String temp = props.getProperty(key);
		if (!AssertUtil.isEmpty(temp)) {
			return Integer.parseInt(temp.trim());
		}
		return 0;
	}

	/**get int
	 * @param key
	 * @param defVal
	 * @return
	 */
	public int getInt(String key, int defVal) {
		String temp = props.getProperty(key);
		if (!AssertUtil.isEmpty(temp)) {
			return Integer.parseInt(temp);
		}
		return defVal;
	}
	
}
